﻿using CZBK.ShopHeima2.DAL;
using CZBK.ShopHeima2.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CZBK.ShopHeima2.BLL
{
    public abstract class BaseService<T> where T : class,new()
    {
        public IDbSession DbSession
        {
            get
            {
                return DbSessionFactory.CreateCurrentDbSession();
            }
        }


        public BaseService()
        {
            SetCurrentRepostitory();
        }

        public IBaseRepository<T> CurrentRepository;
        public abstract void SetCurrentRepostitory();



        # region 查询和分页
        public IEnumerable<T> LoadAllEntities(Func<T, bool> whereLambda)
        {
            return CurrentRepository.LoadAllEntities(whereLambda);
        }

        public IEnumerable<T> LoadAllPageEntities<S>(int pageIndex, int pageSize, Func<T, bool> whereLambda, Func<T, S> orderByLambda, bool isAsc)
        {
            return CurrentRepository.LoadAllPageEntities<S>(pageIndex, pageSize, whereLambda, orderByLambda, isAsc);
        }
        #endregion

        #region cud
        public T AddEntity(T entity)
        {

            return CurrentRepository.AddEntity(entity);
        }

        public bool UpdateEntity(T entity)
        {
            CurrentRepository.UpdateEntity(entity);
            return true;
        }

        public bool DeleteEntity(int id)
        {
            CurrentRepository.DeleteEntity(id);
            return true;
        }

        #endregion


        public int ExecuteSql(string strSql, System.Data.SqlClient.SqlParameter[] paras)
        {
            return DbSession.ExecuteSql(strSql, paras);
        }

        public int SavaChanges()
        {
            return DbSession.SavaChanges();
        }
    }
}
